// 2017/06/14 16:56:12
var storage = require('/storage.js');

/**
 * app 小程序实例
 * timeStamp 点击事件时间
 */
function canTap(e) {
  var time = (new Date()).getTime();
  console.log(time)
  console.log(getApp().globalData.timeStamp)
  if (getApp().globalData.timeStamp + 500 < time) {
    getApp().globalData.timeStamp = time;
    console.log(getApp().globalData.timeStamp)
    return true;
  } else {
    return false;
  }
}

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 2017-06-14 16:56
function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

// 06-14 16:56
function formatDate01(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()

  return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

// 2017-06-14
function formatDate02(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()

  return [year, month, day].map(formatNumber).join('-')
}

// 16:56
function formatDate03(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()

  return [hour, minute].map(formatNumber).join(':')
}


function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 倒计时
function countdown(that) {
  var second = that.data.second
  that.setData({
    showTime: second == 0 ? "重新获取" : second + "s重新获取"
  });
  if (second == 0) {
    if (that.data.size == 11) {
      that.setData({
        hasCode: true
      });
    }
    that.setData({
      second: 60
    });
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    countdown(that);
  }, 1000)
}

function toastNotData() {
  // wx.showToast({
  //   title: "加载完成",
  //  // image: "../../images/icon_failure.png"
  // })
}

// 数据加载出错
function toastError(error) {
  wx.showToast({
    title: error,
    image: "../../images/icon_failure.png"
  })
}

function toast(msg) {
  wx.showToast({
    title: msg,
  })
}

// 评论登录弹出框
function commentLoginDialog(statusId, commentId) {
  if (storage.isLoginFlag()) {
    wx.navigateTo({
      url: "/pages/circle/editComment?statusId=" + statusId + "&commentId=" + commentId
    })
  } else {
    wx.showModal({
      title: "提示",
      content: "检测到您的帐号还未绑定手机号，请绑定手机号。",
      confirmText: "绑定手机",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: "/pages/login/login?pageType=1001&statusId=" + statusId + "&commentId=" + commentId
          })
        }
      }
    })
  }
}

// 支付登录弹出框
function payLoginDialog(that) {
  if (!storage.isLoginFlag()) {
    wx.showModal({
      title: "提示",
      content: "检测到您的帐号还未绑定手机号，请绑定手机号。",
      confirmText: "绑定手机",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: "/pages/login/login?pageType=1003"
          })
        }
      }
    })
  } else {
    wx.navigateTo({
      url: "/pages/details/pay?id=" + that.data.speciesInfo.id,
    })
  }
}

/**
 * 获取屏幕宽高
 */
function windowSize() {
  var windowSize = {};
  //获取屏幕宽高  
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      //console.log(res)
      //console.log('windowWidth: ' + windowWidth)
      //console.log('windowHeight: ' + windowHeight)
      windowSize.winWidth = windowWidth;
      windowSize.winHeight = windowHeight;
    }
  })
  return windowSize;
}

/**
 * 商品优惠价计算，即减除多少钱，单位：分
 * 优惠券可抵金额
 * price：原价
 * coupon：优惠券对象
 */
function concessionalRate(that, price, coupon) {
  console.log(coupon)
  var amount = 0;
  if (coupon.type == "消费折扣") {
    if (price >= coupon.value1) {
      amount = price - Math.round(price * coupon.value2 / 1000)
    } else {
      that.showZanTopTips('商品价格没有达到优惠券使用条件');
    }
  } else if (coupon.type == "消费抵现") {
    if (price > coupon.value1) {
      amount = coupon.value1;
    } else {
      that.showZanTopTips('消费金额须大于优惠券金额');
    }
  } else {
    if (price >= coupon.value1) {
      amount = coupon.value2;
    } else {
      that.showZanTopTips('商品价格没有达到优惠券使用条件');
    }
  }
  return amount;
}

/**
 * 商品优惠价计算，即减除多少钱，单位：分
 * 积分折现
 * integral：积分
 * rate折现率
 */
function integralToAmount(integral, rate) {
  var amount = integral / rate * 100;
  return amount;
}

/**
 * 退款计算,单位为分
 * total 订单总价,单位为分
 * price 被退商品价格,单位为分
 * couponPrice 优惠金额,单位为分
 */
function refund(total, price, couponPrice) {
  var amount = 0;
  amount = Math.round(price - (price / total) * couponPrice)
  return amount;
}

/**
 * 积分、优惠券同时使用的退款计算,单位为分
 * total 订单总价,单位为分
 * price 被退商品价格,单位为分
 * couponPrice 使用优惠券优惠的金额,单位为分
 * integralPrice 使用积分优惠的金额,单位为分
 */
function refund2(total, price, integralPrice, couponPrice) {
  var amount = 0;
  amount = Math.round(price - (price / total) * integralPrice - (price / total) * couponPrice)
  return amount;
}

//金额取两位小数
function toDecimal2(x) {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
  // var sdata = x.toString();
  // var result = sdata.substr(0, sdata.indexOf(".") + 3);
  // return result
}




module.exports = {
  canTap: canTap,
  formatTime: formatTime,
  formatDate: formatDate,
  formatDate01: formatDate01,
  formatDate02: formatDate02,
  formatDate03: formatDate03,
  countdown: countdown,
  toastError: toastError,
  toastNotData: toastNotData,
  commentLoginDialog: commentLoginDialog,
  toast: toast,
  payLoginDialog: payLoginDialog,
  windowSize: windowSize,
  concessionalRate: concessionalRate,
  refund: refund,
  refund2: refund2,
  integralToAmount: integralToAmount,
  toDecimal2: toDecimal2
}
